package de.koller.cell.util.draw.buttons;

import de.koller.aframe.util.math.SphereUtil;
import de.koller.aframe.util.misc.ColorOperations;
import de.koller.oceanFramework.gl.util.ImagePreview;
import de.koller.oceanFramework.math.RVec3;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class RoundEdgeButton {
	
	public static void main( String[] args ) {
		BufferedImage img = new BufferedImage( 128, 32, BufferedImage.TYPE_INT_ARGB );
		new RoundEdgeButton( 6 ).render( img );
		ImagePreview.showImage( img, 128, 32 );
	}

	public static final int STEPS = 256;
	
	private int imgWidth, imgHeight;
	private final int borderSize;
	private boolean down = false;
	
	public RoundEdgeButton( int borderSize ) {
		this.borderSize = borderSize;
	}
	
	public void render( BufferedImage bi ) {
		imgWidth = bi.getWidth() / 2;
		imgHeight = bi.getHeight();
		
		down = false;
		BufferedImage less = renderButton();
		
		down = true;
		BufferedImage down = renderButton();
		
		Graphics2D g = bi.createGraphics();
		g.drawImage( less, 0, 0, null );
		g.drawImage( down, imgWidth, 0, null );
	}
	
	private BufferedImage renderButton() {
		BufferedImage bi = new BufferedImage( imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB );
		for( int x = 0; x < bi.getWidth(); x++ ) {
			for( int y = 0; y < bi.getHeight(); y++ ) {
				int[] c = new int[4];
				for( float dx = 0; dx < 1; dx += 1f / STEPS )
					for( float dy = 0; dy < 1; dy += 1f / STEPS ) {
						int[] tc = ColorOperations.getColorComponents( getRasterValue( x + dx, y + dy ) );
						for( int i = 0; i < c.length; i++ )
							c[i] += tc[i];
					}
				
				for( int i = 0; i < c.length; i++ )
					c[i] /= STEPS*STEPS;
				bi.setRGB( x, y, ColorOperations.getColorWithComponents( c ) );
			}
			System.out.println( x + " / " + imgWidth );
		}
		return bi;
	}
	
	private int getRasterValue( float x, float y ) {
		RVec3 rc = new RVec3();
		
		setRelCoord( borderSize, 0, 0, rc );

		if( x < borderSize )
			setRelCoord( borderSize, x - borderSize, 0, rc );
		if( x > imgWidth - borderSize )
			setRelCoord( borderSize, x - (imgWidth-borderSize), 0, rc );
		if( y < borderSize )
			setRelCoord( borderSize, 0, y - borderSize, rc );
		if( y > imgHeight - borderSize )
			setRelCoord( borderSize, 0, y - (imgHeight-borderSize), rc );
			
		if( x < borderSize && y < borderSize )
			setRelCoord( borderSize, x - borderSize, y - borderSize, rc );
		else if( x > imgWidth - borderSize && y < borderSize )
			setRelCoord( borderSize, x - (imgWidth-borderSize), y - borderSize, rc );
		else if( x > imgWidth - borderSize && y > imgHeight - borderSize )
			setRelCoord( borderSize, x - (imgWidth-borderSize), y - (imgHeight-borderSize), rc );
		else if( x < borderSize && y > imgHeight - borderSize)
			setRelCoord( borderSize, x - borderSize, y - (imgHeight-borderSize), rc );
		
		rc.z = SphereUtil.INSTANCE.getValue( rc.x, rc.y );
		if( rc.z <= 0 ) return 0x00_000000;
		
//		if( !down )
//			rc.z *= 5f;
//		else
//			rc.z *= 3f;
		
		return color( rc );
	}
	
	private void setRelCoord( float r, float dx, float dy, RVec3 dst ) {
		dst.x = 1f / r * dx;
		dst.y = 1f / r * dy;
	}
	
	private int color( RVec3 normal ) {
		normal.normalize();
		RVec3 lv = new RVec3( -2, -1f, 3f ).normalize();
		
		int tc = 0xff_ffffff;
		int ac = 0xff_000000;
		int dc = ColorOperations.diffuseLight( lv, normal, 0xff_ffffff, 0xff_ffffff );
		int sc = ColorOperations.specularLight( lv, normal, new RVec3( 0, 0, 1 ), 0xff_ffffff, 0xff_909090, 50 );
		return ColorOperations.lightColor( tc, ac, dc, sc );
	}
	
}
